package com.woniuxy.repay.controller;

import com.woniuxy.entity.RepaymentOverdue;
import com.woniuxy.entity.RepaymentProtection;
import com.woniuxy.repay.entity.PageBean;
import com.woniuxy.repay.service.RepaymentProtectionService;
import com.woniuxy.utils.ResponseResult;
import com.woniuxy.utils.ResultStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author shichen
 * @create 2021-11-21-20:31
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/protection")
public class RepaymentProtectionController {
    @Resource
    RepaymentProtectionService repaymentProtectionService;

    @GetMapping("/selectall/{pageIndex}")
    public ResponseResult selectAll(@PathVariable("pageIndex") Integer pageIndex, RepaymentProtection repaymentProtection) {
        log.debug("进入方法 selectAll");

        PageBean pageBean = repaymentProtectionService.selectByCondition(repaymentProtection, pageIndex);


        log.debug("结果 : {} ", pageBean);
        ResponseResult responseResult =
                new ResponseResult(200, ResultStatus.HAVE_PERMS, "select ok", pageBean);

        return responseResult;
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestBody RepaymentProtection repaymentProtection) {
        log.debug("repayment protection request : {}", repaymentProtection);
        //=== 校验传过来的参数 start ===
        if (repaymentProtection == null && repaymentProtection.getId() == null) {
            return new ResponseResult(200, ResultStatus.NO_PERMS, "no request param", null);
        }
        //=== 校验传过来的参数 end   ===

        repaymentProtectionService.updateByCondition(repaymentProtection);
        ResponseResult responseResult =
                new ResponseResult(200, ResultStatus.NO_PERMS, "repayment protection update success", null);

        log.debug("response : {}", responseResult);
        return responseResult;
    }

    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody RepaymentProtection repaymentProtection) {

        log.debug("request : {}", repaymentProtection);
        ResponseResult responseResult = null;

        //=== 校验传过来的参数 start ===
        if (repaymentProtection != null) {

            try {
                repaymentProtectionService.insert(repaymentProtection);
                responseResult = new ResponseResult(200, ResultStatus.NO_PERMS, "repaymentProtection Insert Success", null);
            } catch (Exception e) {
                e.printStackTrace();
                responseResult = new ResponseResult(500, ResultStatus.NO_PERMS, "repaymentProtection Insert Error", null);
            }
        }
        return responseResult;
    }
}
